<template>
   <el-form :inline="true" :model="productInfoForm" class="demo-form-inline" ref="productFromRef">
    <el-form-item label="产品名称" prop="name">
      <el-input v-model="productInfoForm.name" placeholder="请输入" clearable />
    </el-form-item>
    <el-form-item label="业务类型" prop="businessType">
      <el-select
        v-model="productInfoForm.businessType"
        placeholder="请选择"
        clearable
      >
        <el-option v-for="item in BusinessTypeList" :key="item.value" :label="item.label" :value="item.value"/>
      </el-select>
    </el-form-item>
    
    <el-form-item>
      <el-button type="primary" @click="searchInfo">搜索</el-button>
      <el-button>重置</el-button>
    </el-form-item>
  </el-form>
  <!-- 按钮 -->
  <el-row class="mb-4 btnBox">
    <el-button type="primary">
      <el-icon><Plus /></el-icon>
      <span @click="createProduct = true">新建</span>
    </el-button>
  </el-row>
  <!-- 表格 -->
  <el-table :data="productInfoTable" style="width: 100%" :cell-style="{textAlign:'center'}" :header-cell-style="{'text-align': 'center',background:'#fafafa',color:'#606266' }">
    <el-table-column type="index" label="序号" width="100" />
    <el-table-column prop="name" label="产品名称" />
    <el-table-column prop="businessType" label="业务类型">
      <template #default="{row}">
          <span  v-if="row.businessType == 'AA'">互联网</span>
      </template>
    </el-table-column>
    <el-table-column prop="updTime" label="最后更新时间" />
    <el-table-column label="操作" width="200px">
      <template #default="row">
        <el-button type="primary" link >修改</el-button>
        <el-popconfirm title="你确定要删除吗?" @confirm="del(row)">
           <template #reference>
             <el-button type="primary" link >删除</el-button>
           </template>
        </el-popconfirm>
       
      </template>
    </el-table-column>
  </el-table>
  <!-- 分页 -->
  <el-pagination
      v-model:current-page="page"
      v-model:page-size="pageSize"
      layout="prev, pager, next, jump, sizes"
      :total="total"
      background 
      class="mt-4"
    />
  <!-- 添加的时候增加的抽屉 -->
  <el-drawer
    v-model="createProduct"
    title="新增产品"
    direction="rtl"
    size="50%"
  >
  <addProductInfo :BusinessTypeList="BusinessTypeList"/>
  <template #footer>
    <el-button @click="createProduct = false">取消</el-button>
    <el-button type="primary">确认</el-button>
  </template>
  </el-drawer>
</template>

<script setup lang="ts"> 
import {ref,onMounted} from "vue"
import { Plus } from "@element-plus/icons-vue";
import { getProductInfoList,businessType,delProductInfo } from "@a/productInfo"
import  addProductInfo from "./components/addProductInfo.vue"
import { ElMessage } from 'element-plus'
defineOptions({
  name: "ProductInformation",
});
const total = ref(0);
const page = ref(1);
const pageSize = ref(10); 
const productFromRef = ref<any>(null)
const createProduct = ref(false)
const productInfoForm = ref({
   name: "",
   businessType: "",
})
// 点击搜索的时候发请求
const searchInfo =()=>{
  getProductInfo(productInfoForm.value)
  productFromRef.value.resetFields()
}
// 定义初始化请求列表
const productInfoTable = ref([])
const getProductInfo = (task:any)=>{
  getProductInfoList(task).then((res:any)=>{
    if(res.code === 10000){
      productInfoTable.value = res.result.records
    }
  })
}
//直接定义请求
const BusinessTypeList:any = ref([])
const getBusinessTypeList = ()=>{
  businessType().then((res:any)=>{
    if(res.code === 10000){
      BusinessTypeList.value = res.result
    }
  })
}
// 定义删除
const del = (val:any)=>{
  delProductInfo(val.row.id).then((res:any)=>{
     if(res.code === 10000){
      ElMessage({
        type: "success",
        message: res.msg,
        onClose: ()=>{
          getProductInfo({})
        }
      })
     }
  })
}
// 初始化的时候发请求
onMounted(()=>{
  getProductInfo({})
  getBusinessTypeList()
})
</script>
<style scoped>
.btnBox{
  margin-top: 15px;
  margin-bottom: 20px;
}
.el-pagination{
  display: flex;
  margin-top: 14px;
  margin-bottom: 14px;
  justify-content: flex-end;
  margin-right: 10px;
}
</style>